package com.rednose.videoplayer.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.rednose.videoplayer.model.IconModel;
import com.rednose.videoplayer.R;

import java.util.ArrayList;

public class PlaybackIconsAdapter extends RecyclerView.Adapter<PlaybackIconsAdapter.ViewHolder> {
    @NonNull
    private ArrayList<IconModel> iconModelsList;
    private Context context;
    private OnItemClickListener mListener;
/*    private OnItemClickListener mListener;*/

    //这是一个接口，用于实现RecyclerView的点击事件,并将点击事件传递到Activity中
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
    //这是一个方法，用于设置点击事件,并将点击事件传递到Activity中
    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }


    public PlaybackIconsAdapter(ArrayList<IconModel> iconModelsList, Context context) {
    this.iconModelsList = iconModelsList;
    this.context = context;
    }

    @Override   //这是一个重写方法，用于创建ViewHolder实例，将icons_layout布局加载进来，并创建ViewHolder实例
    public PlaybackIconsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.icons_layout,parent,false);
        return new ViewHolder(view,mListener);
    }

    @Override
    public void onBindViewHolder(@NonNull PlaybackIconsAdapter.ViewHolder holder, int position) {
        holder.icon.setImageResource(iconModelsList.get(position).getImageView());   //重写onBindViewHolder方法，将iconModelsList中的数据设置到ImageView和TextView中
        holder.iconName.setText(iconModelsList.get(position).getIconTitle());       //重写onBindViewHolder方法，将iconModelsList中的数据设置到ImageView和TextView中
    }

    @Override
    public int getItemCount() {                     //重写getItemCount方法，返回iconModelsList的长度
        return iconModelsList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView iconName;
        ImageView icon;
        public ViewHolder(@NonNull View itemView,OnItemClickListener listener) {
            super(itemView);
            icon = itemView.findViewById(R.id.playback_icon);       //获取ImageView实例,并将其存储在ViewHolder中
            iconName = itemView.findViewById(R.id.icon_title);      //获取TextView实例,并将其存储在ViewHolder中
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {                               //重写onClick方法,当用户点击图标时,触发监听器,并调用onItemClick方法
                    if (listener != null) {
                        int position = getAdapterPosition();                     //获取点击的位置
                        if (position != RecyclerView.NO_POSITION) {             //如果点击的位置不是-1,则调用onItemClick方法
                            listener.onItemClick(position);
                        }
                    }
                }
            });
        }
    }
}
